﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Core.EventBus;

/// <summary>
/// 事件源存储器
/// </summary>
//[Comment("事件源存储器")]
[Table("Sys_EventSourceStorer")]
[SugarTable("Sys_EventSourceStorer", tableDescription: "事件源存储器")]
[SugarIndex("Index_{table}_State", nameof(State), OrderByType.Asc)]
[SugarIndex("Index_{table}_EventId", nameof(EventId), OrderByType.Asc)]
[SugarIndex("Index_{table}_CreationTime", nameof(CreationTime), OrderByType.Desc)]
public class SysEventSourceStorer : Abc.Utils.EntityBaseNoIdentity, IExtensionData
{
    /// <summary>
    /// <summary>
    /// Json数据 
    /// </summary>
    [SugarColumn(ColumnDescription = "扩展字段", ColumnDataType = StaticConfig.CodeFirst_BigString, IsJson = true, IsNullable = true)]
    public Dictionary<string, object>? ExtensionData { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    [Comment("创建时间")]
    public DateTimeOffset CreationTime { get; set; } = DateTimeOffset.Now;

    /// <summary>
    /// 完成时间
    /// </summary>
    [Comment("完成时间")]
    public DateTimeOffset? CompletedTime { get; set; } = DateTimeOffset.Now;

    /// <summary>
    /// 错误信息
    /// </summary>
    [Comment("错误信息")]
    public string Error { get; set; }

    /// <summary>
    ///  事件 Id
    /// </summary>
    [Comment(" 事件 Id")]
    [MaxLength(200)]
    public string EventId { get; set; }

    /// <summary>
    /// 事件承载（携带）数据
    /// </summary>
    [Comment("事件承载（携带）数据")]
    [MaxLength(900)]
    public string Payload { get; set; }

    /// <summary>
    /// 事件处理状态
    /// </summary>
    [Comment("事件处理状态")]
    public EventSourceStorerState State { get; set; }

    //public void Configure(EntityTypeBuilder<SysEventSourceStorer> entityBuilder, DbContext dbContext, Type dbContextLocator)
    //{
    //    //throw new NotImplementedException();
    //    entityBuilder.HasIndex(o => new { o.State });
    //}
}

/// <summary>
/// 事件处理状态
/// </summary>
public enum EventSourceStorerState
{
    未处理 = 0,

    //处理中 = 1,
    已处理 = 2,

    出现错误 = 3,
}